const router = require('koa-router')();
const appointmentService = require('../service/appointmentService');
router.prefix('/api/appointment');


/**
 * @api {post} /api/appointment/searchAppointment 获取预约数据列表
 * @apiName searchAppointment
 * @apiGroup appointment
 *
 * @apiParam {int} page 页码.（page和size必填，其余参数可选）
 * @apiParam {int} size 每页显示数.
 * @apiParam {string} people 预约人.
 * @apiParam {string} phone 联系方式.
 * @apiParam {datetime} startTime 时间范围开始时间.
 * @apiParam {datetime} endTime 时间范围的结束时间.
 * @apiParam {string} serviceName 服务名.
 * @apiParam {string} state 状态.
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：没有数据，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {Object[]} data 获取成功时返回的数据
 * @apiSuccess {String} data.appointment_id 预约主键
 * @apiSuccess {String} data.service_id 服务主键值
 * @apiSuccess {String} data.appointment_people 预约人
 * @apiSuccess {String} data.appointment_phone 联系方式
 * @apiSuccess {String} data.appointment_time 时间
 * @apiSuccess {String} data.appointment_remark 备注
 * @apiSuccess {String} data.appointment_state 状态（-1：已取消，0：待处理，1：已处理）
 * @apiSuccess {time} data.created_time 创建时间
 * @apiSuccess {String} data.created_by 创建人
 * @apiSuccess {time} data.updated_time 更新时间
 * @apiSuccess {String} data.updated_by 更新人
 * @apiSuccess {String} data.is_delete 标识是否被删除（0：否，1：是）
 * @apiSuccess {String} data.service_name 服务名
 */

router.post('/searchAppointment', async (ctx) => {
    try {
        let data = ctx.request.body;
        let people = data.people,
            phone = data.phone,
            startTime = data.startTime,
            endTime = data.endTime,
            serviceName = data.serviceName,
            state = data.state,
            page = data.page,
            size = data.size;
        if (!page || !size) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        if (people || phone || startTime || endTime || serviceName || state) {
            let result, allSize;
            result = await appointmentService.handleSearchAppointment(data);
            allSize = await appointmentService.handleGetSearchSize(data);
            result.allSize = allSize.count;
            ctx.body = result;
        } else {
            let result, allSize;
            result = await appointmentService.handleGetAppointment(data);
            allSize = await appointmentService.handleGetAllSize();
            result.allSize = allSize.count;
            ctx.body = result;
        }

    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/appointment/deleteAppointment 删除预约
 * @apiName deleteAppointment
 * @apiGroup appointment
 *
 * @apiParam {String} id 预约id.
 *
 * @apiSuccess {int} code 返回状态码（1：删除成功，2：删除失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/deleteAppointment', async (ctx) => {
    try {
        let data = ctx.request.body;
        let id = data.id;
        if (!id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await appointmentService.handleDeleteAppointment(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/appointment/editAppointment 处理预约
 * @apiName editAppointment
 * @apiGroup appointment
 *
 * @apiParam {String} id 预约id.
 * @apiParam {String} remark 备注.
 *
 * @apiSuccess {int} code 返回状态码（1：修改成功，2：修改失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 *
 */

router.post('/editAppointment', async (ctx) => {
    try {
        let data = ctx.request.body;
        let remark = data.remark,
            id = data.id;
        if (!remark || !id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
            return;
        }
        let result;
        result = await appointmentService.handleEditAppointment(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});
module.exports = router;